package org.example.blogserver.user.service.Impl;

import cn.hutool.core.util.RandomUtil;
import cn.hutool.crypto.SecureUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.checkerframework.checker.units.qual.A;
import org.example.blogpojo.user.domain.User;
import org.example.blogpojo.user.query.UserQuery;
import org.example.blogserver.user.mapper.UserMapper;
import org.example.blogserver.user.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

@Service
public class UserServiceImpl implements UserService {
    @Autowired
    private UserMapper userMapper;
    @Autowired
    private RedisTemplate<Object, Object> redisTemplate;

    @Override
    public List<User> getAllUser() {
        List<User> allUser = (List<User>)redisTemplate.opsForValue().get("allUser");
        if (allUser == null) {
            allUser = userMapper.getAllUser();
            redisTemplate.opsForValue().set("allUser", allUser);
        }
        return allUser;
    }

    @Override
    public User getUserById(Long id) {
        User user =(User) redisTemplate.opsForValue().get("userId:" + id);
        if (user == null) {
            user = userMapper.getUserById(id);
            redisTemplate.opsForValue().set("userId:" + id, user);
        }
        return user;
    }

    @Override
    public PageInfo<User> queryPage(UserQuery userQuery) {
        System.out.println(userQuery.toString());
        PageHelper.startPage(userQuery.getPageNum(), userQuery.getPageSize());
        List<User> users = userMapper.queryPage(userQuery);
        return new PageInfo<User>(users);
    }

    @Override
    public void insertUser(User user) {
        String s1 = RandomUtil.randomString(32);
        user.setSalt(s1);
        String s = SecureUtil.md5(user.getPassword() + s1);
        user.setPassword(s);
        userMapper.insertUser(user);
        if (redisTemplate.hasKey("allUser")) {
            redisTemplate.delete("allUser");
        }
        if (redisTemplate.hasKey("queryPage")) {
            redisTemplate.delete("queryPage");
        }
    }

    @Override
    public void updateUser(User user) {
        userMapper.updateUser(user);
        if (redisTemplate.hasKey("allUser")) {
            redisTemplate.delete("allUser");
        }
        if (redisTemplate.hasKey("queryPage")) {
            redisTemplate.delete("queryPage");
        }
        if (redisTemplate.hasKey("userId:"+user.getId())){
            redisTemplate.delete("userId:"+user.getId());
        }
    }

    @Override
    public void deleteUser(Long id) {
        userMapper.deleteUser(id);
        if (redisTemplate.hasKey("allUser")) {
            redisTemplate.delete("allUser");
        }
        if (redisTemplate.hasKey("queryPage")) {
            redisTemplate.delete("queryPage");
        }
        if (redisTemplate.hasKey("userId:"+id)){
            redisTemplate.delete("userId:"+id);
        }
    }

    @Override
    public void deleteUserByIds(Long[] ids) {
        userMapper.deleteUserByIds(ids);
        Collection<String> keys = new ArrayList<>(ids.length);
        for (Long id : ids) {
            // 这里假设键的格式是 "prefix:" + id，根据实际情况修改前缀
            keys.add("userId:" + id);
        }
        // 2. 批量删除
        redisTemplate.delete(keys);
    }
}
